<template>
  <div style="font-size: 40px"></div>
</template>

<script setup lang="ts">
import { getCurrentInstance } from "vue";
import { useRouter, useRoute } from "vue-router";
import axios from "axios";
import qs from "qs";

const router = useRouter();
const { query } = useRoute();
const { proxy } = getCurrentInstance() as any;
fetchToken();

type authType = {
  access_token: string;
  token_type: string;
  refresh_token: string;
};
//报错token
function saveAuth(sResponse: authType) {
  localStorage.setItem("auth", JSON.stringify(sResponse));
}
// 获取accesstoken
function fetchToken() {
  const url = window.location.toString();

  if (url.includes("code")) {
    const code = query.code;
    const state = query.state;
    const client_uri = import.meta.env.VITE_APP_BACK + "/back";
    const zhi = qs.stringify({
      code: code,
      state: state,
      grant_type: "authorization_code",
      redirect_uri: client_uri,
    });

    axios
      .post(import.meta.env.VITE_APP_USER + "/oauth2/token", zhi, {
        headers: {
          Authorization: "Basic ZmZ6WDgyNEc5RzpDMXUxRlVpcA==",
        },
      })
      .then((res) => {
        if (res.data.code == 200) {
          saveAuth(res.data.data);

          if (window.localStorage.getItem("redirect") != null) {
            router.replace({
              path: window.localStorage.getItem("redirect") as string,
            });
          } else {
            router.replace("/pcdocument/table");
          }
        } else {
          proxy.$message.error("获取用户认证失败，请重新登录！");
          router.replace("/login");
        }
      })
      .catch((error) => {
        console.info(error);
      });
  }
}
</script>

<style lang="scss" scoped>
div {
  background: url(./images/bg.gif) no-repeat;
  background-size: 100% 100%;
  height: 100vh;
}
</style>
